package admin

import (
	"duoke/internal/dao"
	"duoke/internal/model/entity"
	"duoke/service"
	"duoke/utils/gconv"
	"duoke/utils/gtime"
	"duoke/utils/request"
	"duoke/utils/response"
	"github.com/gin-gonic/gin"
	"github.com/gogf/gf/v2/frame/g"
)

var Coupon = new(couponCtl)

type couponCtl struct{}

func (ctl *couponCtl) MerchantRefund(c *gin.Context) {
	req := request.JsonMap(c)
	id := gconv.String(req["id"])
	if id == "" {
		response.Error(c, "ID缺失", nil)
		return
	}
	var coupon *entity.Coupon
	err := dao.Coupon.Ctx(c).Where("id", id).Where("status", 1).Scan(&coupon)
	if err != nil || coupon == nil {
		response.Error(c, "卡券数据不存在", nil)
		return
	}
	// 计算退款金额
	refundAmount := (coupon.PayAmount / ((coupon.Stock + coupon.Sales) * coupon.ValidNum)) * coupon.Stock * coupon.ValidNum
	// 退款到余额
	result, err := dao.User.Ctx(c).Where("id", coupon.UserId).Unscoped().Increment("money", refundAmount/100)
	if err != nil {
		response.Error(c, err.Error(), nil)
		return
	}
	if rowsCount, err := result.RowsAffected(); err != nil || rowsCount == 0 {
		response.Error(c, "退款到余额失败", nil)
		return
	}
	// 新增余额记录
	result, err = dao.UserMoneyLog.Ctx(c).Data(g.Map{
		"user_id":    coupon.UserId,
		"amount":     refundAmount / 100,
		"status":     1,
		"pay_time":   gtime.TimeStamp(),
		"created_at": gtime.TimeStamp(),
	}).Unscoped().Insert()
	if rowsCount, err := result.RowsAffected(); err != nil || rowsCount == 0 {
		response.Error(c, "余额记录新增失败", nil)
		return
	}
	_, err = dao.Coupon.Ctx(c).Where("id", id).Data(g.Map{"status": -1}).Update()
	if err != nil {
		response.Error(c, err.Error(), nil)
		return
	}
	response.Success(c, "退款成功", nil)
	return
	// 直接退款到微信零钱
	//isSuccess, err := service.WechatPay.RefundV3(c, gconv.Float64(coupon.PayAmount/100), coupon.OrderNo, gconv.Float64(refundAmount/100))
	//if err != nil {
	//	_, _ = dao.Coupon.Ctx(c).Data(g.Map{"fail_msg": err.Error()}).Where("id =?", coupon.Id).Update()
	//	response.Error(c, err.Error(), nil)
	//	return
	//}
	//if isSuccess {
	//	_, _ = dao.Coupon.Ctx(c).Data(g.Map{"status": -1, "fail_msg": "退款金额：" + gconv.String(refundAmount/100)}).Where("id =?", coupon.Id).Update()
	//	response.Success(c, "退款成功", nil)
	//	return
	//} else {
	//	_, _ = dao.Coupon.Ctx(c).Data(g.Map{"fail_msg": "退款失败金额：" + gconv.String(refundAmount/100)}).Where("id =?", coupon.Id).Update()
	//	response.Error(c, "退款失败", nil)
	//	return
	//}
}
func (ctl *couponCtl) UserRefund(c *gin.Context) {
	req := request.JsonMap(c)
	id := gconv.String(req["id"])
	if id == "" {
		response.Error(c, "ID缺失", nil)
		return
	}
	var order *entity.CouponOrder
	err := dao.CouponOrder.Ctx(c).Where("id", id).Where("pay_status", 1).Scan(&order)
	if err != nil || order == nil {
		response.Error(c, "订单数据不存在", nil)
		return
	}
	isSuccess, err := service.WechatPay.RefundV3(c, gconv.Float64(order.PayAmount/100), order.OrderNo, gconv.Float64(order.PayAmount/100))
	if err != nil {
		_, _ = dao.CouponOrder.Ctx(c).Data(g.Map{"fail_msg": err.Error()}).Where("id =?", order.Id).Update()
		response.Error(c, err.Error(), nil)
		return
	}
	if isSuccess {
		_, _ = dao.CouponOrder.Ctx(c).Data(g.Map{"status": -1, "fail_msg": "退款金额：" + gconv.String(order.PayAmount/100)}).Where("id =?", order.Id).Update()
		// 退还积分
		service.User.Score(c, order.UserId, -gconv.Int(order.PayAmount), "卡券退款退还积分")
		response.Success(c, "退款成功", nil)
		return
	} else {
		_, _ = dao.CouponOrder.Ctx(c).Data(g.Map{"fail_msg": "退款失败金额：" + gconv.String(order.PayAmount/100)}).Where("id =?", order.Id).Update()
		response.Error(c, "退款失败", nil)
		return
	}
}
